<?php

class UserAction extends Controller {
	
	function UserAction() {
		parent::Controller ();
	}
	
	/**
	 * 注册新用户
	 */
	function regist() {
		if ($_POST) {
			$form_data = $this->form_data ();
			$this->load->model ( "user/usermodel", "usermodel", true );
			$form_data['password'] = md5($form_data['password']);
			unset ( $form_data ['repasswd'] );
			$form_data ['create_time'] = time ();
			$form_data ['last_login_time'] = time ();
			$this->usermodel->insert_user ( $form_data );
			header( 'Location: ' .base_url(). 'index.php/user/UserAction/regist_success');
		} else {
			$this->load->view ( 'user/regist' );
		}
	}
	
	/**
	 * 注册成功跳转的页面
	 */
	function regist_success() {
		$this->load->view ( 'user/regist_success' );
	}
	
	/**
	 * ajax 校验注册email是否可用
	 */
	function ajax_validate_email() {
		$this->load->model ( "user/usermodel", "usermodel", true );
		$data = $this->usermodel->get_user_by_email($this->input->post('email'));
		if ($data) {
			echo "该email已经被注册、请更换!";
		} else {
			echo "恭喜您可以使用该email注册!";
		}
	}
	
	/**
	 * 登录处理
	 */
	function login() {
		$this->load->library('form_validation');
		$this->load->helper(array('form'));
		if (!$_POST) {
			$this->load->view ( 'user/login' );
		} else {
			//常规校验
			$this->form_validation->set_rules('email', '电子邮件', 'trim|required|valid_email');
			$this->form_validation->set_rules('password', '密码', 'required');
			if ($this->form_validation->run() == FALSE) {
			    $this->load->view ( 'user/login' );
				return;
			}
			//逻辑校验
			$this->form_validation->set_rules('login_error', '', 'callback__login_check');
			if ($this->form_validation->run() == FALSE) {
			    $this->load->view ( 'user/login' );
				return;
			}
			@header( 'Location: ' .base_url(). 'index.php/user/UserAction/user_index');
		}
	}
	
	/**
	 * 登录成功页面
	 */
	function user_index() {
		$this->load->library('session');
		$user_id = $this->session->userdata('user_id');
		if (!$user_id) {
			@header( 'Location: ' .base_url(). 'index.php/user/UserAction/login');
		} else {
			$this->load->view ( 'user/user_index' );
		}
	}
	
	/**
	 * 登录处理的 逻辑校验回调函数
	 */
	function _login_check() {
		$this->load->model ( "user/usermodel", "usermodel", true );
		$data = $this->usermodel->get_user_by_email(trim($this->input->post('email')));
		if ($data) {
			if ($data->password != md5(trim($this->input->post('password')))) {
			$this->form_validation->set_message('_login_check', '用户名或密码错误!');	
			return false;			   
			} 
		} else { 
			$this->form_validation->set_message('_login_check', '用户名或密码错误!');
			return false;				
		}
		
		//加载session对象
		$this->load->library('session');
		$user_data = array("username"=> $data->name, "user_id"=>$data->id);
		$this->session->set_userdata($user_data);
		return true;
	}
	
	function exit_system() {
		$this->load->library('session');
		$this->session->sess_destroy();
		redirect('user/userAction/login');
	}
	
	private function form_data() {
		$form_data = array ();
		if ($_POST) {
			foreach ( $_POST as $k => $v ) {
				$form_data [$k] = $v;
			}
		}
		return $form_data;
	}
}
/* End of file UserAction.php */
/* Location: ./system/application/controllers/UserAction.php */